
import React from 'react';
import * as THREE from 'three';
import { Card, Form, InputNumber } from "antd";
import { useElement } from "tools/hooks/useElement";
import { useProperty } from 'tools/hooks/useProperty';
// import { useProperty } from 'tools/hooks/useProperty';

type Light = THREE.DirectionalLight | THREE.SpotLight | THREE.PointLight | undefined

/**
 * 该类作为其他阴影类的基类来使用
 **/
export default function LightShadowCard() {

    const { element } = useElement();

    const light = React.useMemo<Light>(() => {
        if (!element) return undefined
        if (element.type === 'light') {
            return element.object3D as THREE.DirectionalLight;
        }
    }, [element])

    const shadow = React.useMemo<THREE.LightShadow | undefined>(() => {
        if (light instanceof THREE.DirectionalLight) {
            return light.shadow;
        } else if (light instanceof THREE.SpotLight) {
            return light.shadow;
        }else if (light instanceof THREE.PointLight) {
            return light.shadow;
        } else {
            return undefined;
        }
    }, [light])

    const { setProperty } = useProperty(shadow)

    if (!shadow) return <></>;
    return (
        <Card title="阴影参数">
            <Form
                layout="horizontal"
                labelCol={{ span: 9 }}
                wrapperCol={{ span: 15 }}
            >
                <Form.Item label='贴图偏差' tooltip='阴影贴图偏差，在确定曲面是否在阴影中时，从标准化深度添加或减去多少' >
                    <InputNumber
                        size='small'
                        bordered={false}
                        value={shadow.bias}
                        style={{ width: '100%' }}
                        onChange={setProperty('bias')}
                    />
                </Form.Item>
            </Form>
        </Card>
    )
}